package com.he.controller;

import com.he.domain.Result;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.MenuDTO;
import com.he.domain.vo.MenuOption;
import com.he.domain.vo.MenuTree;
import com.he.domain.vo.MenuVO;
import com.he.domain.vo.RouterVO;
import com.he.service.MenuService;
import com.he.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/10/3 10:31
 * @Desc 菜单控制器
 */
@Tag(name = "菜单模块")
@RestController
public class MenuController {
    @Resource
    private MenuService menuService;

    @Resource
    private UserService userService;

    /**
     * 获取登录用户菜单
     *
     * @return {@link RouterVO} 登录用户菜单
     */
    @Operation(description = "获取登录用户菜单")
    @GetMapping("/admin/user/getUserMenu")
    public Result<List<RouterVO>> getUserMenu() {
        return Result.success(userService.getUserMenu());
    }

    /**
     * 获取菜单下拉树
     *
     * @return {@link MenuTree} 菜单树
     */
    @Operation(description = "获取菜单下拉树")
    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @GetMapping("/admin/menu/getMenuTree")
    public Result<List<MenuTree>> listMenuTree() {
        return Result.success(menuService.listMenuTree());
    }


    /**
     * 查看菜单列表
     *
     * @return {@link MenuVO} 菜单列表
     */
    @Operation(description = "查看菜单列表")
    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @GetMapping("/admin/menu/list")
    public Result<List<MenuVO>> listMenuVO(ConditionDTO condition) {
        return Result.success(menuService.listMenuVO(condition));
    }

    /**
     * 获取菜单下拉树
     *
     * @return {@link MenuTree} 菜单树
     */
    @Operation(description = "获取菜单下拉列表")
    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @GetMapping("/admin/menu/getMenuOptions")
    public Result<List<MenuOption>> listMenuOption() {
        return Result.success(menuService.listMenuOption());
    }

    /**
     * 添加菜单
     *
     * @return {@link Result<>}
     */
    @Operation(description = "添加菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:add')")
    @PostMapping("/admin/menu/add")
    public Result<?> addMenu(@Validated @RequestBody MenuDTO menu) {
        menuService.addMenu(menu);
        return Result.success();
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单id
     * @return {@link Result<>}
     */

    @Operation(description = "删除菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:delete')")
    @DeleteMapping("/admin/menu/delete/{menuId}")
    public Result<?> deleteMenu(@PathVariable("menuId") Integer menuId) {
        menuService.deleteMenu(menuId);
        return Result.success();
    }

    /**
     * 编辑菜单
     *
     * @param menuId 菜单id
     * @return {@link MenuDTO} 菜单
     */
    @Operation(description = "编辑菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:edit')")
    @GetMapping("/admin/menu/edit/{menuId}")
    public Result<MenuDTO> editMenu(@PathVariable("menuId") Integer menuId) {
        return Result.success(menuService.editMenu(menuId));
    }


    /**
     * 修改菜单
     *
     * @return {@link Result<>}
     */
    @Operation(description = "修改菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:update')")
    @PutMapping("/admin/menu/update")
    public Result<?> updateMenu(@Validated @RequestBody MenuDTO menu) {
        menuService.updateMenu(menu);
        return Result.success();
    }


}
